<template>
  <ele-page flex-table>
    <!-- 搜索表单 -->
    <PageSearch @search="reload" />

    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        v-model:selections="selections"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        cache-key="ArchivesUnitTable"
      >
        <template #toolbar>
          <el-button type="primary" class="ele-btn-icon" :icon="Plus" @click="openEdit()">
            新增
          </el-button>
        </template>
        <template #status="{ row }">
          <el-tag v-if="row.status === 1">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
        <template #action="{ row }">
          <el-link type="danger" underline="never" @click="remove(row)">
            删除
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="primary" underline="never" @click="openEdit(row)">
            编辑
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <PageEdit v-model="showEdit" :templateList="templateList" :data="current" @done="reload" />
  </ele-page>
</template>

<script setup>
import { Plus } from '@element-plus/icons-vue'
import { ref } from 'vue'
import PageEdit from './components/edit.vue'
import PageSearch from './components/search.vue'

const [templateList] = utils.useGetList('/common/noticeTemp')

/** 表格实例 */
const tableRef = ref(null)

/** 表格列配置 */
const columns = ref([
  {
    type: 'index',
    label: '序',
    width: 50,
    align: 'center',
    fixed: 'left',
  },
  {
    prop: 'name',
    label: '消息组别',
    width: 140,
  },
  {
    prop: 'nt_name',
    label: '消息模版',
    minWidth: 200,
  },
  {
    slot: 'status',
    label: '状态',
    align: 'center',
    formatter: row => row.status === 1 ? '正常' : '禁用',
  },
  {
    prop: 'create_time',
    label: '创建时间',
    formatter: (row) => {
      return utils.formatDate(row.create_time)
    },
    align: 'center',
  },
  {
    prop: 'update_time',
    label: '更新时间',
    formatter: (row) => {
      return row.update_time ? utils.formatDate(row.update_time) : '-'
    },
    align: 'center',
  },
  {
    columnKey: 'action',
    label: '操作',
    align: 'right',
    slot: 'action',
    fixed: 'right',
    showOverflowTooltip: false,
    width: 120,
  },
])

/** 表格选中数据 */
const selections = ref([])

/** 当前编辑数据 */
const current = ref(null)

/** 是否显示编辑弹窗 */
const showEdit = ref(false)

/** 表格数据源 */
function datasource({ page, limit, where, orders }) {
  return utils.get('/noticeGroup/index', { ...where, ...orders, page, limit })
}

/** 搜索 */
function reload(where) {
  selections.value = []
  tableRef.value?.reload?.({ page: 1, where })
}

/** 打开编辑弹窗 */
function openEdit(row) {
  current.value = row ?? null
  showEdit.value = true
}

/** 删除单个 */
function remove(row) {
  utils.confirm(
    `确定要删除“${row.name}”吗?`,
    '系统提示',
    { type: 'warning', draggable: true },
  )
    .then(() => {
      const loading = utils.message('请求中..', 'loading')
      utils.post('/noticeGroup/delete', { id: row.id })
        .then((res) => {
          utils.message(res.msg, res.success)
          reload()
        })
        .finally(() => loading.close())
    })
    .catch(() => {})
}
</script>
